package com.beaven.imclient.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.activity.MainActivity;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.presenter.account.LoginContract;
import com.beaven.imclient.presenter.account.LoginPresenter;
import com.beaven.imclient.utils.ToastUtil;
import com.beaven.imclient.widget.DesEdit;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment
    implements View.OnClickListener, LoginContract.View {

  private OnLoginInteractionListener listener;

  public static LoginFragment newInstance() {
    return new LoginFragment();
  }

  private DesEdit edit_phone_login;
  private DesEdit edit_pwd_login;
  private LoginContract.Presenter presenter;
  private ProgressDialog dialog;

  @Override public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnLoginInteractionListener) {
      listener = (OnLoginInteractionListener) context;
    } else {
      throw new RuntimeException(context.toString() + " must implement OnLoginInteractionListener");
    }
  }

  @Override protected int getLayoutId() {
    return R.layout.fragment_login;
  }

  @Override protected void initView(View rootView) {
    super.initView(rootView);
    edit_phone_login = (DesEdit) rootView.findViewById(R.id.edit_phone_login);
    edit_pwd_login = (DesEdit) rootView.findViewById(R.id.edit_pwd_login);
    edit_pwd_login.getEditText()
        .setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
    Button bt_login = (Button) rootView.findViewById(R.id.bt_login);
    TextView text_register = (TextView) rootView.findViewById(R.id.text_register);

    bt_login.setOnClickListener(this);
    text_register.setOnClickListener(this);
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new LoginPresenter(this));
  }

  @Override public void onResume() {
    super.onResume();
    presenter.subscribe();
  }

  @Override public void onPause() {
    super.onPause();
    presenter.unSubscribe();
  }

  @Override public void onDetach() {
    super.onDetach();
    listener = null;
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.bt_login:
        presenter.login();
        break;
      case R.id.text_register:
        listener.toggleFragment();
        break;
      default:
        break;
    }
  }

  @Override public void setPresenter(LoginContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public String getAccount() {
    return edit_phone_login.getEditText().getText().toString().trim();
  }

  @Override public String getPassword() {
    return edit_pwd_login.getEditText().getText().toString().trim();
  }

  @Override public void showLoading() {
    dialog = ProgressDialog.show(getActivity(), "", getString(R.string.load_now));
  }

  @Override public void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
  }

  @Override public void showText(int strId) {
    ToastUtil.show(strId);
  }

  @Override public void startMain() {
    MainActivity.start(getContext());
    getActivity().finish();
  }

  public interface OnLoginInteractionListener {
    void toggleFragment();
  }
}
